/**
 * Created by Administrator on 2018/10/21.
 */
var http = require('http');
var express = require('express');
var querystring = require('querystring');
const modeUrl = require("url");
var axios = require('axios');
axios.defaults.withCredentials = true
var app = express();

app.get('/apimusic/qqyydiss', function (reqFist, resFist) {

// "c.y.qq.com"
// "https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg"
// "https://y.qq.com/portal/playlist.html"
  //var postData = querystring.stringify({
  //    'content': '测试成功了',
  //    'cid': 348   // 课程编号
  //});
  var params = querystring.stringify(reqFist.query);
  var options = {
      hostname: "140.207.69.99",
      port: 80,
      path: '/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?' + params,
      method: 'GET',
      headers: {
          'host': 'c.y.qq.com',
          'referer': 'https://y.qq.com/portal/playlist.html',
          // 'authority': 'c.y.qq.com',
          // 'method': 'GET',
          // 'scheme': 'https',
          // 'accept-encoding': 'gzip, deflate, br',
          'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
      }
  }

  var req = http.request(options, function(res) {
      // console.log('Status: ' + res.statusCode);
      // console.log('Headers: ' + JSON.stringify(res.headers));
      var reData = ''
      res.on('data', function(chunk) {
          // console.log(Buffer.isBuffer(chunk));
          // console.log(typeof chunk);
          reData += chunk;
          
      });
      res.on('end', function() {
          console.log('请求完毕');
          // console.log(reData);
          resFist.send(reData);
      });
  });
  req.on('error', function(e) {
      console.log('Error: ' + e.message);
  });
  // req.write();
  req.end();
  // resFist.json( {abc: 'def'} );

});


///////////////////////////////////////////////////////////////////////////
/*
app.get('/apiqqyysinger', function (reqFist, resFist) {
  var params = querystring.stringify(reqFist.query);
  var options = {
      // https://u.y.qq.com/cgi-bin/musicu.fcg
      
      hostname: "140.207.123.197",
      port: 80,
      path: '/cgi-bin/musicu.fcg?' + params,
      method: 'GET',
      headers: {
          'host': 'u.y.qq.com',
          'referer': 'https://y.qq.com/portal/singer_list.html',
          'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
      }
  }
  var req = http.request(options, function(res) {
      var reData = ''
      res.on('data', function(chunk) {
          reData += chunk;
      });
      res.on('end', function() {
          console.log('/apiqqyysinger 请求完毕');
          resFist.send(reData);
      });
  });
  req.on('error', function(e) {
      console.log('Error: ' + e.message);
  });
  req.end();
});
*/


////////////////////////////////////////////////////////////////////////////////
// 获取歌词
app.get('/apimusic/qqyylyric', function (req, res) {
  var url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg';
  
  axios.get(url, {
    headers: {
      host: 'c.y.qq.com',
      referer: 'https://y.qq.com/portal/player.html'
    },
    params: req.query
  }).then((response) => {
    var ret = response.data
    if (typeof ret === 'string') {
      var reg = /^\w+\(({[^()]+})\)$/
      var matches = ret.match(reg)
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
});

// 获取歌单列表
app.get('/apimusic/getcdinfo', function (req, res) {
  var url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg';
  
  axios.get(url, {
    headers: {
      host: 'c.y.qq.com',
      referer: 'https://y.qq.com/?ADTAG=myqq'
    },
    params: req.query
  }).then((response) => {
    var ret = response.data
    if (typeof ret === 'string') {
      var reg = /^\w+\(({[^()]+})\)$/
      var matches = ret.match(reg)
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
});

// 获取 输入关键字查询结果
app.get('/apimusic/getsuggests', function (req, res) {
  var url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp';
  
  axios.get(url, {
    headers: {
      host: 'c.y.qq.com',
      referer: 'https://y.qq.com/m/index.html'
    },
    params: req.query
  }).then((response) => {

    var ret = response.data
    if (typeof ret === 'string') {
      var reg = /^\w+\(({.+})\)$/
      var matches = ret.match(reg)
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
});


// 获取 输入关键字查询结果
// app.get('/test/jsonp', function (req, res) {
//   var url = 'http://www.xuezhiwuyou.com/Api/Course/course_info.html?course_id=12';
  
//   axios.get(url, {
//     headers: {
//     },
//     params: req.query
//   }).then((response) => {
//     var ret = response.data
//     if (typeof ret === 'string') {
//       var reg = /^\w+\(({.+})\)$/
//       var matches = ret.match(reg)
//       if (matches) {
//         ret = JSON.parse(matches[1])
//       }
//     }
//     res.json(ret)
//   }).catch((e) => {
//     console.log(e)
//   })
// });

app.listen(3000, '192.168.1.102');
console.log('listen 3000');
